define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'usercar/index',
                    add_url: 'usercar/add',
                    edit_url: 'usercar/edit',
                    del_url: 'usercar/del',
                    multi_url: 'usercar/multi',
                    table: 'user_car',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'car_id',
                sortName: 'car_id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'user_id', title: __('用户ID')},
                        {field: 'phone', title: __('注册手机号')},
                        {field: 'brand_name', title: __('品牌')},
                        {field: 'type_name', title: __('车型')},
                        {field: 'color_name', title: __('车身颜色')},
                        {field: 'user_address', title: __('居住地')},
                        {
                            field: 'driving_license_img', 
                            title: __('行驶证'),
                            formatter: Controller.api.formatter.car_img
                        },
                        {field: 'remark', title: __('备注')},
                        {field: 'createtime', title: __('申请时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'checktime', title: __('通过时间'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'car_id', 
                            title: __('操作'), 
                            operate: false, 
                            width: '80px',
                            formatter: Controller.api.formatter.opt
                        },
//                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });
            
            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#','');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    params.filter = JSON.stringify({car_cert: typeStr});
//                    params.status = typeStr;
                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;
            });
            
            $(table).on("click", "[data-id].btn-refuse", function (e) {
                e.preventDefault();
                var id = $(this).data("id");
                var that = this;
                var url = $(that).data("url");
                Layer.prompt({title: __('拒绝原因'), formType: 0}, function (value, index) {
                    Backend.api.ajax({
                        url: url+"?car_id="+id+"&refuse_text="+value,
                        data: $(that).closest("form").serialize()
                    },function(){
                        table.bootstrapTable('refresh', {});
                    });
                    Layer.close(index);
                });
            });
            
            $(table).on("click", "[data-id].btn-check", function (e) {
                e.preventDefault();
                var id = $(this).data("id");
                var that = this;
                var url = $(that).data("url");
                Layer.confirm(
                    __('确定通过该申请吗？'),
                    {icon: 3, title: __('提示'), shadeClose: true},
                    function (index) {
                        $.post(url,{car_id:id},function(res){
                            table.bootstrapTable('refresh', {});
                        });
                        Layer.close(index);
                    }
                );
            });
            
            //初始化默认列表
            var options = table.bootstrapTable('getOptions');
            options.pageNumber = 1;
            options.queryParams = function (params) {
                params.filter = JSON.stringify({car_cert: 0});
                return params;
            };
            table.bootstrapTable('refresh', {});
            
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            formatter: {//渲染的方法
                car_img: function (value, row, index) {
                    var html = '';
                    $.each(value,function(i,v){
                        html += '<a href="'+v+'" target="_blank"><img src="'+v+'" style="max-width: 80px; max-height: 60px;"/></a>';
                    });
                    return html;
                },
                opt:function (value, row, index) {
                    var html = '';
                    if(row.car_cert == 0){
                        html += '<a class="text-success btn-check"  style="cursor:pointer" data-url="usercar/checkok" data-id="'+ row.car_id + '"><b>同意</b></a> | ';
                        html += '<a class="text-success btn-refuse"  style="cursor:pointer" data-url="usercar/checkno" data-id="'+ row.car_id + '"><b>拒绝</b></a>';
                    }else{
                        html += '-';
                    }
                    return html;
                }
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
